/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.workplan;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.attach.AttachLink;
import com.unis.model.workplan.Workplan;
import com.unis.service.attach.AttachmentService;
import com.unis.service.workplan.WorkplanService;

/**
 * @author Administrator (email:xxh@163.com)
 * @since 2013-03-20
 */
@Controller
@RequestMapping("/workplan")
public class WorkplanController {

    private WorkplanService workplanService;
    private AttachmentService attachmentService;
    
    /****************************************
     * Insert a instace of Workplan
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertWorkplan(@RequestBody Workplan workplan) throws Exception {
        workplan.setStatmentId("Workplan.insert");
        String status = "failure";
        workplanService.insert(workplan);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of Workplan
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Workplan workplan) throws Exception {
        workplan.setStatmentId("Workplan.update");
        String status = "failure";
        workplanService.update(workplan);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of Workplan
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody Workplan workplan) throws Exception {
        workplan.setStatmentId("Workplan.delete");
        String status = "failure";
        workplanService.batchDelete(workplan);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of Workplan
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        Workplan workplan = new Workplan();
        workplan.setStatmentId("Workplan.queryAsList");
        BeanUtils.populate(workplan, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(workplan, pageMap);
        List<BaseModel> workplanList = workplanService.queryAsList(workplan);
        Map<String, Object> renderMap = GridUIMapper.render(workplan, workplanList);
        return renderMap;
    }

    /****************************************
     * Query a instaces of Workplan
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody Workplan workplan) throws Exception {
        workplan.setStatmentId("Workplan.queryAsObject");
        return workplanService.queryAsObject(workplan);
    }
    
    @RequestMapping("/queryWorkplanAttach")
    @ResponseBody
    public Object queryWorkplanAttach(Workplan workplan) throws Exception {
        AttachLink attachlink = new AttachLink();
        attachlink.setEntityid(workplan.getPlanid());
        return attachmentService.querybyEntityid(attachlink);
    }

    @RequestMapping("/page")
    public String forword() {
        return "workplan/workplanList";
    }

    @Resource
    public void setWorkplanService(WorkplanService workplanService) {
        this.workplanService = workplanService;
    }
    
    @Resource
    public void setAttachmentService(AttachmentService attachmentService) {
        this.attachmentService = attachmentService;
    }
}
